package com.magina.antiPro.config;

import com.magina.antiPro.handler.AsyncExpectionHandler;
import com.magina.antiPro.handler.GobalExpectionHandler;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
    /*
    此处成员变量应该使用@Value从配置中读取
    */
//    private int corePoolSize = 10;
//    private int maxPoolSize = 200;
//    private int queueCapacity = 10;

    @Value(value = "${MaginaData.corePoolSize}")
    private int corePoolSize;
    @Value(value = "${MaginaData.maxPoolSize}")
    private int maxPoolSize;
    @Value(value = "${MaginaData.queueCapacity}")
    private int queueCapacity;
    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("Magina_Test-");
        System.out.println("corePoolSize:"+corePoolSize);
        executor.initialize();
        return executor;
    }
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new AsyncExpectionHandler();
    }
}